package com.epam.web_project.bank.command.show_commands;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ShowUnapprovedCommand implements Command {

	//show main page for unapproved customer
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		HttpSession session = request.getSession();
		if (!session.isNew()) {
			Customer customer = (Customer) session.getAttribute("customer");
			MySQLDAOFactory daoFactory = new MySQLDAOFactory();
			daoFactory.setConnectionPool(ConnectionPool.getInstance());
			AccountDAO accountDAO = daoFactory.getAccountDAO();
			List<Account> accountsUnapproved = accountDAO.findByCustomerId(customer
					.getId(),true);
			List<Account> accountsApproved = accountDAO.findByCustomerId(customer
					.getId(),false);
			if (accountsUnapproved != null && !accountsUnapproved.isEmpty()) {
				request.setAttribute("accountsUnapproved", accountsUnapproved);
			}
			if (accountsApproved != null && !accountsApproved.isEmpty()) {
				request.setAttribute("accountsApproved", accountsApproved);
			}
		}
		request.setAttribute("message", session.getAttribute("message"));
		session.removeAttribute("message");
		return props.getProperty("unapproved_page");
	}

}
